#!/usr/bin/env python
###############################################################################
# mainwindow.py: VMWrapper's MainWindow
###############################################################################
# Copyright (C) 2010 CERN
#
# Authors: Rohit Yadav <rohityadav89 AT gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
###############################################################################

import wx
import sys
from widgets import *

class WrapperGUI(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size = (540, 200))

        self.timer = wx.Timer(self, 1)
        self.count = 0

        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)

        centerPanel      = wx.Panel(self, -1)
        boxMain          = wx.BoxSizer(wx.HORIZONTAL)

        self.creditLabel = wx.StaticText(centerPanel, -1, 'Credit')
        self.vmCPU       = CPUWidget(centerPanel, -1)
        self.vmRAM       = CPUWidget(centerPanel, -1, '#310007', '#ff2736')
        self.vmCPULabel  = wx.StaticText(centerPanel, -1, 'VM CPU Usage')
        self.vmRAMLabel  = wx.StaticText(centerPanel, -1, 'VM RAM Usage')

        self.logTextBox = wx.TextCtrl(centerPanel, -1, '', size=(300, 100), style=wx.TE_MULTILINE | wx.HSCROLL | wx.VSCROLL | wx.TE_READONLY)

        fgs = wx.FlexGridSizer(3, 2, 5, 5)
        fgs.AddMany([(self.creditLabel, 1, wx.EXPAND), ((10, 0), 1),
                     (self.vmCPU, 0, wx.ALIGN_CENTER), (self.vmRAM, 0, wx.ALIGN_CENTER, 100),
                     (self.vmCPULabel), (self.vmRAMLabel)])

        boxMain.Add(fgs, 0, wx.ALL | wx.ALIGN_CENTER | wx.EXPAND, 15)
        boxMain.Add(self.logTextBox, 2, wx.ALL | wx.EXPAND, 10)

        centerPanel.SetSizer(boxMain)

        self.Centre()
        self.Show(True)
        self.timer.Start(100)

    def debug(self, token, value):
       self.logTextBox.AppendText("[" + token + "]: " + value + "\n")
       sys.stderr.write('[' + str(token) + ']: ' + str(value) + "\n") 

    def OnTimer(self, event):
        self.vmCPU.setValue(self.count + 1)
        self.vmRAM.setValue(self.count + 1)
        self.count += 1

        if self.vmCPU.pos == 100:
            self.timer.Stop()

def test():
    app = wx.App()
    p = WrapperGUI(None, -1, 'BoincVM Wrapper')
    p.debug("test", "yeah")
    p.debug("test2", "yeah2")
    app.MainLoop()

if __name__ == "__main__":
    test()

